#include <bits/stdc++.h>

using namespace std;
using ll = long long;
#define int long long

void solve()
{
    int n;
    cin >> n;
    string s;
    cin >> s;
    int cnt = count(s.begin(), s.end(), '3');
    if (cnt == 0)
    {
        cout << 0;
        return;
    }
    else if (cnt == 1)
    {
        int pos = s.find('3');
        if (pos == n - 1)
        {
            cout << 0;
            return;
        }
        int pos1 = s.rfind('8');
        cout << pos1 - pos - 1;
        return;
    }
    int pos1 = s.find('3');
    int pos2 = s.rfind('3');
    int pos = s.rfind('8');
    int ans = pos2 - pos1;
    int pos3 = s.rfind('3', pos + 1);
    if (pos3 == pos1)
    {
        cout << ans;
        return;
    }
    cout << pos - pos1;
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int _ = 1;
    //cin >> _;
    while (_--)
    {
        solve();
    }
    return 0;
}